package com.oct.website.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.oct.website.entity.Avgthi;
import com.oct.website.entity.TravelDataResultView;
import com.oct.website.mapper.AvgThiMapper;
import com.oct.website.mapper.ScenicMapper;
import com.oct.website.mapper.TravelDataResultMapper;
import com.oct.website.util.DateUtil;

@Controller
@RequestMapping("/index")
public class IndexController {

	Logger logger = Logger.getLogger("IndexController.java");
	@Autowired
	private HttpServletRequest request; //自动注入request
	
	@Autowired
	private TravelDataResultMapper travelDataResultMapper;
	
	@Autowired
	private AvgThiMapper avgThiMapper;
	
	@Autowired
	private ScenicMapper scenicMapper;
	
	@RequestMapping("")
	public ModelAndView index(Model model) throws Exception {
		
		ModelAndView mv = new ModelAndView("index");
		
		// 查询环球旅游快讯
		List<TravelDataResultView> travelList1 = this.travelDataResultMapper.selectList("国内资讯");// 国内
		logger.info("国内资讯："+JSONArray.toJSONString(travelList1));
		List<TravelDataResultView> travelList2 = this.travelDataResultMapper.selectList("地方资讯");// 地方
		List<TravelDataResultView> travelList3 = this.travelDataResultMapper.selectList("全球资讯");// 全球
		List<TravelDataResultView> travelList4 = this.travelDataResultMapper.selectList("国别资讯");// 国别
		List<TravelDataResultView> travelList5 = this.travelDataResultMapper.selectList("其他分类");// 其他分类
		
		mv.addObject("travelList1", travelList1);
		mv.addObject("travelList2", travelList2);
		mv.addObject("travelList3", travelList3);
		mv.addObject("travelList4", travelList4);
		mv.addObject("travelList5", travelList5);
		
		
		// 查询两年的指数数据
		int curYear = DateUtil.getSysYear();
		List<Avgthi> thiList = this.avgThiMapper.selectTwoYearData(curYear);
		List<Avgthi> lastYearList = thiList.stream().filter(s->s.getMonthStr().indexOf(""+(curYear-1))==0).collect(Collectors.toList());
		List<Avgthi> curYearList = thiList.stream().filter(s->s.getMonthStr().indexOf(""+(curYear))==0).collect(Collectors.toList());
		//logger.info("2019年数据："+JSONArray.toJSONString(lastYearList));
		//logger.info("2020年数据："+JSONArray.toJSONString(curYearList));
		
		List<BigDecimal> avgThiList_last = lastYearList.stream().map(Avgthi::getAvgThi).collect(Collectors.toList());// 去年均值
		List<BigDecimal> avgThiList_cur = curYearList.stream().map(Avgthi::getAvgThi).collect(Collectors.toList());  // 今年均值
		BigDecimal[] rates = new BigDecimal[avgThiList_cur.size()];
		
		for(int i=0;i<avgThiList_cur.size(); i++) {
			rates[i] = avgThiList_cur.get(i).subtract(avgThiList_last.get(i)).divide(avgThiList_last.get(i), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
		}
		/*logger.info("2019年数据-统计数值："+JSONArray.toJSONString(avgThiList_last));
		logger.info("2020年数据-统计数值："+JSONArray.toJSONString(avgThiList_cur));
		logger.info("2020年数据-同比数值："+JSONArray.toJSONString(rates));*/
		
		
		mv.addObject("thiSta_cls", new String[] {(curYear-1)+"年", curYear+"年", "同比"});   // 比如：["2019年"，"2010年"，"同比"]
		mv.addObject("avgthiList1", avgThiList_last);
		mv.addObject("avgthiList2", avgThiList_cur);
		mv.addObject("avgthiRates", rates);
		
		return mv;
	}
	
	/**
	 * 景区查询联想功能
	 */
	@RequestMapping("/scenicThinkData")
	@ResponseBody
	public Object scenicThinkData() {
		String scenicName = request.getParameter("scenicName");
		
		List scenicList = this.scenicMapper.selectScenicNameList(scenicName);
		logger.info(scenicName + "; scenicList:"+ JSONArray.toJSONString(scenicList));
		return scenicList;
	}
	
	/**
	 * 景区精确查询功能
	 */
	@RequestMapping("/scenicPreciseData")
	@ResponseBody
	public Object scenicPreciseData() {
		String scenicName = request.getParameter("scenicName");
		
		int ct = this.scenicMapper.selectScenicNamePrecise(scenicName);
		logger.info(scenicName + "; 查询数量为:"+ ct);
		return ct;
	}
	
	
}

